package com.kaili.common.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：文件帮助类
 * @author: dxj
 * @descrinption: Created by dxj on 18-6-25.
 * @modified:
 */
public class FileUtils {

    public static List<String> getAttachUploadFileType(){
        List<String> list = new ArrayList<String>();
        list.add("jpg");
        list.add("jpeg");
        list.add("bmp");
        list.add("gif");
        list.add("png");
        return list;
    }

    /**
     *复制文件夹和文件夹下的所有文件
     * @param sourcePath
     * @param newPath
     * @throws IOException
     */
    public static void copyDir(String sourcePath, String newPath){
        File file = new File(sourcePath);
        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdir();
        }

        for (int i = 0; i < filePath.length; i++) {
            if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
                copyDir(sourcePath  + file.separator  + filePath[i], newPath  + file.separator + filePath[i]);
            }

            if (new File(sourcePath  + file.separator + filePath[i]).isFile()) {
                Path target = Paths.get(sourcePath + file.separator + filePath[i]);
                Path source = Paths.get(newPath + file.separator + filePath[i]);
                try{
                    Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
                }catch(Exception e){
                }
            }

        }
    }
}
